﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business;
using System.Globalization;

namespace blackhouse.WebNemo.Web.Controls {
    public partial class EditReferences : System.Web.UI.UserControl {

        #region Fields

        private int groupId;
        private int parentGroupId;
        private int parentRecordId;
        private string parentColumnName;
        private int[] selectedRecordsIdx;

        private GroupRecord refRecord;
        private GroupColumn refColumn;
        private IEnumerable<GroupRecord> selectedRecords;
        private IEnumerable<GroupRecord> nonselectedRecords;

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.ReadParameters();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.SetInstance();
            this.PopulateControls();
        }

        #endregion

        #region Methods

        private void ReadParameters() {
            Int32.TryParse(Request["groupId"], out this.parentGroupId);
            Int32.TryParse(Request["recordId"], out this.parentRecordId);
            Int32.TryParse(Request["refGroupId"], out this.groupId);

            this.parentColumnName = Request["refColumn"];

            this.selectedRecordsIdx = new int[0];

            List<string> selecteds = new List<string>();
            if (!string.IsNullOrEmpty(Request["selected"])) {
                string[] s = Request["selected"].Split(',');
                if (s.Length > 0)
                    selecteds.AddRange(s);

            }
            if (!string.IsNullOrEmpty(Request[this.hidSelectedRecords.UniqueID])) {
                string[] s = Request[this.hidSelectedRecords.UniqueID].Split(',');
                if (s.Length > 0)
                    selecteds.AddRange(s);
            }

            if (selecteds.Count > 0) {
                List<int> selectedsCheck = new List<int>(selecteds.Count);

                for (int xs = 0; xs < selecteds.Count; xs++) {
                    int val = 0;
                    if (Int32.TryParse(selecteds[xs], out val))
                        selectedsCheck.Add(val);
                }
                this.selectedRecordsIdx = selectedsCheck.ToArray();
            }
        }

        private void SetInstance() {
            this.refRecord = new GroupRecord(this.parentGroupId, this.parentRecordId);
            this.refColumn = this.refRecord.GetGroup().Columns[this.parentColumnName];

            IEnumerable<GroupRecord> records = new GroupRecords(this.groupId);
            this.selectedRecords = from r in records join s in this.selectedRecordsIdx on r.RecordId equals s select r;
            this.nonselectedRecords = records.Except(this.selectedRecords);
        }

        private void PopulateControls() {
            this.ltlIdx.Text = this.refRecord.RecordId.ToString();
            this.ltlBaseIdentyficator.Text = this.refRecord.Identyficator;
            this.ltlBaseColumn.Text = this.refColumn.Name;

            if (this.refColumn.ColumnType == FieldType.OneReference) {
                this.recSelected.OneSelect = true;
                this.recNonSelected.OneSelect = true;
                this.ltlSelected.Text = "Wybrana pozycja";
            }

            if (this.selectedRecords.Count() > 0) {
                this.recSelected.DataBind(this.selectedRecords);
                this.pnlSelected.Visible = true;
            }
            else
                this.pnlSelected.Visible = false;

            if (this.nonselectedRecords.Count() > 0) {
                this.recNonSelected.DataBind(this.nonselectedRecords);
                this.pnlNonSelected.Visible = true;
            }
            else
                this.pnlNonSelected.Visible = false;

            bool addPanelVisible = this.selectedRecords.Count() == 0 && this.nonselectedRecords.Count() == 0;

            this.pnlNoRecords.Visible = addPanelVisible;
            this.lnkToAddNew2.Visible = !addPanelVisible;
            this.hidSelectedRecords.Visible = addPanelVisible;

            NemoUrl url = NemoUrl.Get();
            url.View = NemoUrlView.EditRecord;
            url.GroupId = this.groupId;
            url.RecordId = 0;
            url.AutoClose = true;
            this.lnkToAddNew.NavigateUrl = url.GetUrl();
            this.lnkToAddNew2.NavigateUrl = this.lnkToAddNew.NavigateUrl;
            this.hidWrapGroup.Value = this.groupId.ToString();
        }

        #endregion

    }
}
